package com.wukong.alive;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.Layout;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;


import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;


public class MainActivity extends Activity {


    Switch sw_isopen;//是否开启应用
    ImageView app_image;
    TextView app_name;

    Switch sw_isopen_timer;
    //    LinearLayout timer_1_layout;
//    LinearLayout timer_2_layout;
//    LinearLayout timer_3_layout;
//    LinearLayout timer_4_layout;
//    LinearLayout timer_5_layout;
//
    TextView timer_1_value;
    TextView timer_2_value;
    TextView timer_3_value;
    TextView timer_4_value;
    TextView timer_5_value;


    Switch sw_isopen_reboot;
    //    LinearLayout timer_1_layout;
//    LinearLayout timer_2_layout;
//    LinearLayout timer_3_layout;
//    LinearLayout timer_4_layout;
//    LinearLayout timer_5_layout;
//
    TextView reboot_1_value;
    TextView reboot_2_value;
    TextView reboot_3_value;
    TextView reboot_4_value;
    TextView reboot_5_value;


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "当前无权限，请授权", Toast.LENGTH_SHORT).show();
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
        }
        initView();

        refApp();

    }

    void initView() {
        app_image = findViewById(R.id.app_image);
        app_name = findViewById(R.id.app_name);
        sw_isopen = findViewById(R.id.sw_isopen);
        if (CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_isOpenApp, "false").equals("true")) {
            sw_isopen.setChecked(true);
        }
        sw_isopen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String value = "false";
                if (isChecked) {
                    value = "true";
                }
                CacheDiskUtils.getInstance().put(CacheKey.cacheKey_isOpenApp, value);
            }
        });

        sw_isopen_timer = findViewById(R.id.sw_isopen_timer);
        if (CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_isOpenTimer, "false").equals("true")) {
            sw_isopen_timer.setChecked(true);
        }
        sw_isopen_timer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String value = "false";
                if (isChecked) {
                    value = "true";
                }
                CacheDiskUtils.getInstance().put(CacheKey.cacheKey_isOpenTimer, value);
            }
        });

        //系统是否重启
        sw_isopen_reboot = findViewById(R.id.sw_isopen_reboot);
        if (CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_isOpenReboot, "false").equals("true")) {
            sw_isopen_reboot.setChecked(true);
        }
        sw_isopen_reboot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String value = "false";
                if (isChecked) {
                    value = "true";
                }
                CacheDiskUtils.getInstance().put(CacheKey.cacheKey_isOpenReboot, value);
            }
        });



        findViewById(R.id.selete_app).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SelectAppActivity.class);
                startActivityForResult(intent, 1000);
            }
        });

        findViewById(R.id.timer_1_layout).setOnClickListener(timerSelectClick);
        findViewById(R.id.timer_2_layout).setOnClickListener(timerSelectClick);
        findViewById(R.id.timer_3_layout).setOnClickListener(timerSelectClick);
        findViewById(R.id.timer_4_layout).setOnClickListener(timerSelectClick);
        findViewById(R.id.timer_5_layout).setOnClickListener(timerSelectClick);


        timer_1_value = findViewById(R.id.timer_1_value);
        timer_2_value = findViewById(R.id.timer_2_value);
        timer_3_value = findViewById(R.id.timer_3_value);
        timer_4_value = findViewById(R.id.timer_4_value);
        timer_5_value = findViewById(R.id.timer_5_value);

        timer_1_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_timer1,""));
        timer_2_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_timer2,""));
        timer_3_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_timer3,""));
        timer_4_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_timer4,""));
        timer_5_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_timer5,""));



        findViewById(R.id.reboot_1_layout).setOnClickListener(rebootSelectClick);
        findViewById(R.id.reboot_2_layout).setOnClickListener(rebootSelectClick);
        findViewById(R.id.reboot_3_layout).setOnClickListener(rebootSelectClick);
        findViewById(R.id.reboot_4_layout).setOnClickListener(rebootSelectClick);
        findViewById(R.id.reboot_5_layout).setOnClickListener(rebootSelectClick);

        reboot_1_value = findViewById(R.id.reboot_1_value);
        reboot_2_value = findViewById(R.id.reboot_2_value);
        reboot_3_value = findViewById(R.id.reboot_3_value);
        reboot_4_value = findViewById(R.id.reboot_4_value);
        reboot_5_value = findViewById(R.id.reboot_5_value);

        reboot_1_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_reboot1,""));
        reboot_2_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_reboot2,""));
        reboot_3_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_reboot3,""));
        reboot_4_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_reboot4,""));
        reboot_5_value.setText(CacheDiskUtils.getInstance().getString(CacheKey.cacheKey_reboot5,""));
    }

    View.OnClickListener timerSelectClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String timerNum = v.getTag().toString();
            int timerId = getResources().getIdentifier("timer_" + timerNum + "_value", "id", getApplicationContext().getPackageName());
            TextView timerValueView = findViewById(timerId);

            TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                            Calendar cal = Calendar.getInstance();
                            cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                            cal.set(Calendar.MINUTE, minute);
                            String timeValue = new SimpleDateFormat("HH:mm").format(cal.getTime());
                            String cackeKey = "timer_" + timerNum;

                            CacheDiskUtils.getInstance().put(cackeKey, timeValue);
                            timerValueView.setText(timeValue);
                        }
                    }, 0, 0, true
            );
            timePickerDialog.show();
        }
    };


    View.OnClickListener rebootSelectClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String timerNum = v.getTag().toString();
            int timerId = getResources().getIdentifier("reboot_" + timerNum + "_value", "id", getApplicationContext().getPackageName());
            TextView timerValueView = findViewById(timerId);

            TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                            Calendar cal = Calendar.getInstance();
                            cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                            cal.set(Calendar.MINUTE, minute);
                            String timeValue = new SimpleDateFormat("HH:mm").format(cal.getTime());
                            String cackeKey = "reboot_" + timerNum;

                            CacheDiskUtils.getInstance().put(cackeKey, timeValue);
                            timerValueView.setText(timeValue);
                        }
                    }, 0, 0, true
            );
            timePickerDialog.show();
        }
    };

    /**
     * 刷新应用信息
     */
    void refApp() {
        String appPackage = CacheDiskUtils.getInstance().getString("appPackage");
        if (!StringUtils.isEmpty(appPackage)) {
            AppUtils.AppInfo info = AppUtils.getAppInfo(appPackage);
            app_image.setImageDrawable(info.getIcon());
            app_name.setText(info.getName());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == Activity.RESULT_OK) {
            String pname = data.getStringExtra("package");
            CacheDiskUtils.getInstance().put("appPackage", pname);
            refApp();
        }
    }
}